package com.bom.core.util.db;

import jetbrick.template.JetEngine;
import jetbrick.template.JetTemplate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.UtilityClass;

import java.io.StringWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@UtilityClass
public class TbFactory {
    public static class Tb {
        @Getter
        private String name;
        @Getter
        private String desc;
        @Getter
        private Map<String, Column> colsMap = new LinkedHashMap<>();
        @Getter
        private Map<String, Index> indexsMap = new LinkedHashMap<>();
        @Getter
        private String primary;

        public Tb(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }

        public Tb addCol(String[] col) {
            colsMap.put(col[0], new Column(col[0], col[1], col[2]));
            return this;
        }

        public Tb addCols(String[][] cols) {
            for (String[] col : cols) {
                colsMap.put(col[0], new Column(col[0], col[1], col[2]));
            }
            return this;
        }

        public Tb addIndex(boolean unique, String... names) {
            Index index = new Index();
            index.setTbName(name);
            index.setNodes(Stream.of(names).map(e -> new IndexNode(e, null)).collect(Collectors.toList()));
            index.setName(name + "_" + index.getNodes().stream().map(IndexNode::getName).collect(Collectors.joining("_")));
            index.setUnique(unique);
            indexsMap.put(index.getName(), index);
            return this;
        }

        public Tb addPrimary(String name) {
            this.primary = name;
            return this;
        }
    }

    public Tb builder(String tbName, String desc) {
        return new Tb(tbName, desc);
    }

    public String createTb(Tb tb) {
        // 1. 创建一个默认的 JetEngine
        JetEngine engine = JetEngine.create();

        // 2. 获取一个模板对象 (从默认的 classpath 下面)
        JetTemplate template = engine.getTemplate("/tb/postgres/createTb");

        // 3. 创建 context 对象
        Map<String, Object> context = new HashMap<>();
        context.put("tb", tb);

        // 4. 渲染模板到自定义的 Writer
        StringWriter writer = new StringWriter();
        template.render(context, writer);
        return writer.toString();
    }

    @Data
    public static class Index {
        private String tbName;
        private String name;
        private boolean unique;
        private List<IndexNode> nodes = new LinkedList<>();
    }

    @Data
    @AllArgsConstructor
    public static class IndexNode {
        private String name;
        private String order;
    }

    @Data
    public static class Column {
        private String name;
        private String type;
        private String desc;

        public Column(String name, String type, String desc) {
            this.name = name;
            this.type = type;
            this.desc = desc;
        }
    }
}
